<script lang="ts" setup>
import { deleteCarAPI, getAreaListAPI } from '@/apis/car';
import router from '@/router';
import type { AreaPage, Row } from '@/types/car';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ref } from 'vue';
//分页信息
const areaPage = ref<AreaPage>({
  page: 1,
  pageSize: 10,
  cardStatus: undefined,
  carNumber: undefined,
  personName: undefined
})
//获取月卡列表
const getAreaList = async () => {
  const res = await getAreaListAPI(areaPage.value)
  areaList.value = res.data.rows
  total.value = res.data.total
}
getAreaList()
//定义月卡列表
const areaList = ref<Row[]>([])
//月卡数量
const total = ref(0)
//删除月卡
const deleteCar = (id: string) => {
  ElMessageBox.confirm('此操作将永久删除该月卡, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    try {
      await deleteCarAPI(id)
      getAreaList()
      ElMessage.success('删除成功！')
    } catch (err: any) {
      ElMessage.error(err.response.data.msg)
    }
  }).catch(() => { })
}
//表格多选框选中的列表
const selectionList= ref<Row[]>([])
//表格多选框改变
const handleSelectionChange = (val: Row[]) => {
  selectionList.value = val
}
//删除被选中的月卡
const delListCar = () => {
  ElMessageBox.confirm('此操作将永久删除选中月卡, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    try {
      await deleteCarAPI(selectionList.value.map(item=>item.id).join(','))
      getAreaList()
      ElMessage.success('删除成功！')
    } catch (err: any) {
      ElMessage.error(err.response.data.msg)
    }
  }).catch(()=>{})
}
</script>
<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input clearable placeholder="请输入内容" class="search-main" v-model="areaPage.carNumber" />
      <span class="search-label">车主姓名：</span>
      <el-input clearable placeholder="请输入内容" class="search-main" v-model="areaPage.personName" />
      <span class="search-label">状态：</span>
      <el-select style="width: 240px" v-model="areaPage.cardStatus" clearable>
        <el-option label="全部" value="2" />
        <el-option label="可用" value="0" />
        <el-option label="已过期" value="1" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="getAreaList">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/addcar')">添加月卡</el-button>
      <el-button type="primary" @click="delListCar">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="areaList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态">
          <template #default="scope">
            <el-tag v-if="scope.row.cardStatus === 0" type="success">可用</el-tag>
            <el-tag v-else type="danger">已过期</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{ row }">
            <el-button size="small" type='primary' link>续费</el-button>
            <el-button size="small" type='primary' link>查看</el-button>
            <el-button size="small" type='primary' link @click="router.push(`/addcar/${row.id}`)">编辑</el-button>
            <el-button size="small" type='primary' link @click="deleteCar(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination layout="total,sizes, prev, pager, next ,jumper" :total="total"
        v-model:page-size="areaPage.pageSize" v-model:current-page="areaPage.page" @change="getAreaList"
        :page-sizes="[2, 5, 10, 20]" />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>